package com.ZeroCarbon.BusinessUtils;

import com.ZeroCarbon.client.UserClient;
import com.ZeroCarbon.entity.Result;
import com.ZeroCarbon.method.RemoteClientCall;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
 * UserClient远程调用业务方法处理
 *
 * @since 2025-2-12
 */
public class UserClientUtils {

    //用户头像key
    public static final String AVATAR = "avatar";

    //用户名key
    public static final String USERNAME = "username";

    /**
     * 远程调用用户信息（用户名、头像）
     */
    public static Map<String, String> getUserClientInformation(String id, RemoteClientCall.OperationType... type) {
        return Stream.of(type)
                .collect(HashMap::new, (map, operationType) -> {
                    //获取方法名
                    String method = operationType.getValue();
                    //获取返回结果
                    Result result = RemoteClientCall.getClientResult(
                            new Class[]{UserClient.class},
                            operationType,
                            Result.class,
                            new Object[]{id}
                    );
                    //获取By的索引位置
                    int byIndex = method.indexOf("By");
                    //获取key
                    String key = method.substring("get".length(), byIndex == -1 ? method.length() : byIndex).toLowerCase();
                    //将结果放入map
                    map.put(key, (String) result.getData());
                }, HashMap::putAll);
    }
}
